
import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { Scene } from "../Scene";
import { BattleResultType } from "../model/BattleEnum";
import { BattleAutoComp } from "./BattleAuto";

/** 战场动画播放器 */
@ecs.register('BattleAnimator')
export class BattleAnimatorComp extends ecs.Comp {
    onComplete: Function = null!;

    reset() {
        this.onComplete = null!;
    }
}

export class BattleAnimatorSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem {
    filter(): ecs.IMatcher {
        return ecs.allOf(BattleAnimatorComp);
    }

    entityEnter(e: Scene): void {
        this.play(e);
    }
    private play(e: Scene) {
        var reports = e.MapModelRole.reports;
        if (reports.length > 0) {
            var data = reports.shift()!;
            data.skill.play(data, this.play.bind(this, e))
        }
        else {
            this.exit(e);
        }
    }
    private exit(e: Scene) {
        if (e.BattleModel.battleResultType == BattleResultType.Win) {
            e.BattleModel.battleResultType = null!;
            e.battleEnd(true);
            e.remove(BattleAutoComp);
        }
        else if (e.BattleModel.battleResultType == BattleResultType.Lose) {
            e.BattleModel.battleResultType = null!;
            e.battleEnd(false);
            e.remove(BattleAutoComp);
        } else {
            e.get(BattleAnimatorComp)?.onComplete();
        }

        e.remove(BattleAnimatorComp);
    }
}